<?php


global $drivercount, $state, $drivers;

$drivercount = 0;
$state = '';
$drivers = Array();
	
function compare_drivers($a, $b)
{
	return $a['best3'] > $b['best3'];
}


class qualifying_results
{

	var $drivers = Array();
	var $title = '';
	


	function qualifying_results($file, $cut_off_time, $title)
	{
		global $drivers;
		
		$this->title = $title;
		
		if (!($fp=@fopen("../" . $file, "r"))) 
			die ("Couldn't open XML.");
		
		
		if (! ($xml_parser = xml_parser_create()) )
		{ 
		   die ("Cannot create parser");
		}
		xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
		xml_set_character_data_handler( $xml_parser, "characterDataHandler");
		
		while( $data = fread($fp, 4096))
		{
			if(!xml_parse($xml_parser, $data, feof($fp)))
			{
				echo $data . ' ' . sprintf('XML error %d:"%s" at line %d column %d byte %d', 
						xml_get_error_code($xml_parser), 
						xml_error_string($xml_parser), 
						xml_get_current_line_number($xml_parser), 
						xml_get_current_column_number($xml_parser), 
						xml_get_current_byte_index($xml_parser));
				break;
			}
		}
					
		xml_parser_free($xml_parser);
		
		foreach($drivers as $key=>$driver)
		{
			$lc = 0;
			$lc_tot = 0;
			$lc_last_tot = 999999999999999;
			$drivers[$key]["Name"] = $driver["Name"] . '<br>' . $driver["Car"];
			for($x = 0; $x < sizeof($driver["Laps"]); $x++)
			{
				if(is_numeric($driver["Laps"][$x]))
				{
					$lc++;
					$lc_tot += $driver["Laps"][$x];
					$drivers[$key]["Laps"][$x] = formatTimestamp($driver["Laps"][$x] * 1000, 'i:s');
				}
				else
				{
					$lc = 0;
					$lc_tot = 0;
					$drivers[$key]["Laps"][$x] =  '---';
				}
				
				if($lc == 3)
				{
					if($lc_tot < $lc_last_tot)
					{
						$lc_last_tot = $lc_tot;
					}
					
					$lc--;
					$lc_tot -= $driver["Laps"][$x - 2];
				}
			}
			$drivers[$key]['best3'] = $lc_last_tot;
		}
		
		usort($drivers, "compare_drivers");
		
		foreach($drivers as $id => $driver)
		{

			$drivers[$id]["on"] = 'Off';
			
			$laps = '';
			$last_break = true;
			foreach($driver["Laps"] as $lap)
			{
				if($lap == '---' && ! $last_break)
				{
					$laps .=  '<br>';
					$last_break = true;
				}
				else if($lap != '---')
				{
					$last_break = false;
					$laps .=  $lap . ' ';
				}
			}
			
			$drivers[$id]['Laps'] = substr($laps, 0, -2);
			if($driver["best3"] == 999999999999999)
				$drivers[$id]["best3"] = '';
			else
			{
				if($drivers[$id]["best3"] < $cut_off_time)
					$drivers[$id]["on"] = 'On';
					
				$drivers[$id]["best3"] = formatTimestamp($drivers[$id]["best3"] * 1000, 'i:s');
			}
		}
		
		$this->drivers = $drivers;
	}
	
	function render()
	{
		global $lang, $skin_dir;
		
		include_once('includes/addMessage.php');
		
		$addReview = new addMessage('', $this->title);
		$addReview->render();
		
		include $skin_dir . 'tpl/tableStart.tpl';
		
		foreach($this->drivers as $driver)
		{
			include $skin_dir . 'tpl/qualifyingResult.tpl';
		}
		
		include $skin_dir . 'tpl/tableEnd.tpl';
	}
}


?>